<?php
class WeiboInfo_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return thatWeiboUserInfo
     */
    public static function getWeiboUserInfoByRow($row)
    {
        $thatWeiboUserInfo = new Weibo;
        $thatWeiboUserInfo->setId($row->id);
        $thatWeiboUserInfo->setUserId($row->user_id);
        $thatWeiboUserInfo->setName($row->name);
        $thatWeiboUserInfo->setToken($row->token);
        $thatWeiboUserInfo->setWeiboId($row->weibo_id);
        $thatWeiboUserInfo->setPhotos($row->photos);
        $thatWeiboUserInfo->setGender($row->gender);
        $thatWeiboUserInfo->setDescription($row->description);
        $thatWeiboUserInfo->setData_json($row->data_json);
        $thatWeiboUserInfo->setLocation($row->location);
        return $thatWeiboUserInfo;
    }

    /**
     * @param WeiboUserInfo $thatWeiboUserInfo
     * @return array
     */
    public static function getModelByUserInfo($thatWeiboUserInfo)
    {
        return array(
            'user_id'     => $thatWeiboUserInfo->getUserId(),
            'name'        => $thatWeiboUserInfo->getName(),
            'token'       => $thatWeiboUserInfo->getToken(),
            'weibo_id'    => $thatWeiboUserInfo->getWeiboId(),
            'photos'      => $thatWeiboUserInfo->getPhotos(),
            'gender'      => $thatWeiboUserInfo->getGender(),
            'description' => $thatWeiboUserInfo->getDescription(),
            'data_json'   => $thatWeiboUserInfo->getData_json(),
            'location'    => $thatWeiboUserInfo->getLocation()
        );
    }

    /**
     * @param int $id
     * @throws FormatException
     * @return resault
     */
    public static function getUserById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('WeiboInfo.*');
        $CI->db->from('WeiboInfo');
        $CI->db->where('WeiboInfo.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getWeiboUserInfoByRow($row);
        else
            return null;

    }

    /**
     * @param int $userId
     * @throws FormatException
     * @return User
     */
    public static function getWeiboByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('WeiboInfo.*');
        $CI->db->from('WeiboInfo');
        $CI->db->where('WeiboInfo.user_id', $userId);
        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getWeiboUserInfoByRow($row);
        else
            return null;
    }

    public static function getByWeiboId($weibo_id)
    {
         /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('WeiboInfo.*');
        $CI->db->from('WeiboInfo');
        $CI->db->where('WeiboInfo.weibo_id', $weibo_id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getWeiboUserInfoByRow($row);
        else
            return null;
    }

    /**
     * @param User $thatWeiboUserInfo
     * @throws DatabaseException
     * @return id
     */
    public static function add($thatWeiboUserInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('WeiboInfo',self::getModelByUserInfo($thatWeiboUserInfo));
        if($result)
            return self::getUserById($CI->db->insert_id());
        else
            return false;
    }
}